package com.gin.kotlin.no_gay_alliance.model

import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.DATE_TIME_FORMATTER_MONTH_SECOND
import com.gin.kotlin.no_gay_alliance.utils.TimeUtils.Companion.FULL_DATE_TIME_FORMATTER
import com.gin.nga.enums.FromClient
import com.gin.nga.enums.VoteType
import com.gin.nga.enums.status.AccountStatus
import com.gin.nga.response.reply.ReplyInfo
import java.time.ZonedDateTime

/**
 * 回复模型
 * @property block 是否被屏蔽
 * @property userId 用户名
 * @property topicId 主题id
 * @property light 背景色为浅色 或 深色
 * @property isWarden 是否为Warden（应用沃顿蓝）
 * @property replyId 回复ID
 * @property username 用户名
 * @property anonymous 是否匿名
 * @property avatarBuff 头像buffID
 * @property avatar 随机头像
 * @property accountStatus 账号状态(HTML)
 * @property attachments 附件
 * @property floor 楼层号
 * @property client 客户端
 * @property prestige 威望组(威望值)
 * @property reputation 声望
 * @property postCount 发帖数
 * @property medals 徽章URL
 * @property gifts 礼物
 * @property title 标题
 * @property punishedInTopic 是否在主题内被处罚
 * @property replyStatus 回复状态
 * @property postTime 发布时间
 * @property agreeCount 赞数
 * @property lastEdit 最后修改
 * @property logs 操作记录
 * @property comment 评论
 * @property hotReplies 热评
 * @property muted 是否在某版面被禁言
 * @property score 评分回复的评分值
 * @constructor
 */
class ReplyModel(replyInfo: ReplyInfo) {
    val block = App.INSTANCE.blockConfig?.isBlocked(replyInfo) ?: false
    val userId = replyInfo.authorUid
    val topicId = replyInfo.topicId
    val light = (replyInfo.floorNumber?.rem(2) ?: 0) == 0
    val isWarden = replyInfo.userInfo?.groupName == "Warden"
    val replyId = replyInfo.replyId
    val content = replyInfo.content
    val username = replyInfo.anonymousName ?: replyInfo.userInfo?.username
    val anonymous = replyInfo.isAnonymous()
    val avatarBuff = replyInfo.userInfo?.getAvatarBuffId()?.let { App.INSTANCE.avatarBuffs?.firstOrNull { buff -> buff.id == it } }
    val avatar = replyInfo.userInfo?.getAvatars()?.random()
    val accountStatus = replyInfo.userInfo?.getAccountStatus()?.let {
        when (it) {
            AccountStatus.PHONE, AccountStatus.LINK -> ""
            else -> "<span style='color:${it.color}'>[${it.cn}]</span>"
        }
    } ?: ""
    val attachments = replyInfo.attachments?.map { AttachmentModel(it) }
    val floor = replyInfo.floorNumber
    val client = when (replyInfo.client) {
        FromClient.IOS, FromClient.Android, FromClient.PC -> replyInfo.client?.name
        else -> ""
    } ?: ""

    val prestige = replyInfo.userInfo?.prestige?.div(10.0)?.let { "${replyInfo.userInfo?.groupName}: $it" } ?: ""
    val reputation = run {
        val level = replyInfo.userInfo?.reputationLevel ?: "声望"
        replyInfo.userInfo?.reputationValue?.let { "$level (${replyInfo.userInfo?.reputationValue})" }
    } ?: ""
    val postCount = replyInfo.userInfo?.postCount?.let { "发帖 $it" } ?: ""
    val medals = replyInfo.userInfo?.medals?.mapNotNull { it.getMedalUrl() }
    val gifts = replyInfo.gifts
    val title = replyInfo.title ?: ""
    val punishedInTopic = replyInfo.punishedInTopic
    val replyStatus = ReplyStatusModel.parse(replyInfo)
    val postTime =
        replyInfo.postDatetime?.format(if (ZonedDateTime.now().year == replyInfo.postDatetime?.year) DATE_TIME_FORMATTER_MONTH_SECOND else FULL_DATE_TIME_FORMATTER) ?: ""
    val agreeCount = replyInfo.agreeCount?.toString() ?: "0"

    /**
     * 最后修改
     */
    val lastEdit = replyInfo.alterInfo?.lastEdit?.let {
        val user = it.username?.let { i -> "由 $i" } ?: ""
        "修改:${it.timestamp?.format(if (ZonedDateTime.now().year == replyInfo.postDatetime?.year) DATE_TIME_FORMATTER_MONTH_SECOND else FULL_DATE_TIME_FORMATTER)} $user"
    } ?: ""
    val logs = replyInfo.alterInfo?.logs
    val comment = replyInfo.comment
    val hotReplies = replyInfo.hotReplies
    val muted = replyInfo.userInfo?.isMuted() ?: false

    // 投票信息
    val voteData = replyInfo.getVoteData()
    val score = voteData?.takeIf { it.type == VoteType.SCORE_REPLY }?.score
    val vote = replyInfo.vote

}